layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 练习示例表管理
     */
    var OrderTransport = {
        tableId: "transportMapTable"
    };

    /**
     * 初始化表格的列
     */
    OrderTransport.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'customerName', sort: true, title: '客户',width:250},
            {field: 'getLocation', title: '收货地址',width:150},
            {field: 'lineName', title: '线路'},
            {field: 'totalVolume', title: '体积', totalRow: true},
            {field: 'totalNum', title: '数量', totalRow: true},
            {field: 'tstate', sort: true, title: '订单状态',templet: '#tstateTpl',
                templet: function(d){
                    var str = "";
                    if (d.tstate == '已发运'){
                        str += '<span style="color:#ffaa00;font-weight: bold;">' + d.tstate + '</span>'
                    } else if (d.tstate == '已完成'){
                        str += '<span style="color:#19be6b;font-weight: bold;">' + d.tstate + '</span>'
                    } else if (d.tstate == '作废'){
                        str += '<span style="color:#e84d74;font-weight: bold;">' + d.tstate + '</span>'
                    } else {
                        str += '<span style="font-weight: bold;">' + d.tstate + '</span>'
                    }
                    return str
                }
            },
            {field: 'stockPort', sort: true, title: '备货口'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    OrderTransport.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(OrderTransport.tableId, {
            where: queryData, page: {curr: 1},
            done:function () {
            }
        });
    };

    /**
     * 弹出添加对话框
     */
    OrderTransport.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/lxDemo/add';
    };

    /**
     * 导出excel按钮
     */
    OrderTransport.exportExcel = function () {
        var checkRows = table.checkStatus(OrderTransport.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    OrderTransport.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/lxDemo/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    OrderTransport.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/lxDemo/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(OrderTransport.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };


    var tableResult = table.render({
        elem: '#' + OrderTransport.tableId,
        url: Feng.ctxPath + '/orderTransport/orderMap',
        page: true,
        limit: 40,
        limits:[40,60,120,240,360,480,1200],
        height: 600,
        cellMinWidth: 100,
        toolbar: '#toolbarfrom',

        cols: OrderTransport.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        OrderTransport.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        OrderTransport.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        OrderTransport.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + OrderTransport.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            OrderTransport.openEditDlg(data);
        } else if (layEvent === 'delete') {
            OrderTransport.onDeleteItem(data);
        }
    });

    $.get({
        url:"/basicAddress/mapAddress",

        success: function (data){
            for (var i = 0; i < data.length; i++) {
                console.log(data);
                var point = new BMapGL.Point(data[i].longitude, data[i].latitude);
                var markerPoint = new BMapGL.Marker(point);
                map.addOverlay(markerPoint);

                // 创建图文信息窗口
                var sContent = `<h4 style='margin:0 0 5px 0;'>货运订单</h4>
                <p style='margin:0;line-height:1.5;font-size:13px;text-indent:2em'>
                门店：` + data[i].putgoodsName + `
                </p>
                <p style='margin:0;line-height:1.5;font-size:13px;text-indent:2em'>
                体积：` + data[i].totalVolume + `
                </p></div>`;
                var infoWindow = new BMapGL.InfoWindow(sContent);
                // marker添加点击事件
                markerPoint.addEventListener('click', function () {
                    map.openInfoWindow(infoWindow, point); //开启信息窗口
                })
            }
        }
    })

});
